namespace C1.Win.C1FlexGrid
{
    using C1.Win.C1FlexGrid.Util;
    using System;
    using System.Collections;

    internal class GridRowComparer : IComparer
    {
        private bool _asDisplayed;
        private int _col1;
        private int _col2;
        private bool _forceString;
        internal GridModel _grid;
        private bool _ignoreCase;
        private bool _noSort;
        private SortFlags _order;
        private bool _reverse;

        internal GridRowComparer(GridModel grid, SortFlags order, int colFrom, int colTo)
        {
            this._grid = grid;
            this._col1 = colFrom;
            this._col2 = colTo;
            this._order = order;
            this._forceString = false;
            this._noSort = false;
        }

        public int Compare(object x, object y)
        {
            int num = 0;
            Row row = (Row) x;
            Row row2 = (Row) y;
            if (row.Equals(row2))
            {
                return 0;
            }
            this.UpdateFlags(this._order);
            for (int i = this._col1; i <= this._col2; i++)
            {
                if ((this._order & SortFlags.UseColSort) != SortFlags.None)
                {
                    this.UpdateFlags(this._grid._cols[i].Sort);
                }
                if (!this._noSort)
                {
                    if (this._asDisplayed || this._forceString)
                    {
                        string dataDisplay = this._grid.GetDataDisplay(row.Index, i);
                        string s = this._grid.GetDataDisplay(row2.Index, i);
                        if ((this._forceString && Types.IsNumeric(dataDisplay)) && Types.IsNumeric(s))
                        {
                            double num3 = Types.ParseDouble(dataDisplay);
                            double num4 = Types.ParseDouble(s);
                            num = (num3 > num4) ? 1 : ((num3 < num4) ? -1 : 0);
                        }
                        else
                        {
                            num = string.Compare(dataDisplay, s, this._ignoreCase);
                        }
                    }
                    else
                    {
                        object data = this._grid.GetData(row.Index, i);
                        object obj3 = this._grid.GetData(row2.Index, i);
                        num = ((data is string) && (obj3 is string)) ? string.Compare((string) data, (string) obj3, this._ignoreCase) : Types.Compare(data, obj3);
                        if (num < -1)
                        {
                            this._forceString = true;
                            return this.Compare(x, y);
                        }
                    }
                    if (num != 0)
                    {
                        break;
                    }
                }
            }
            if (this._reverse)
            {
                num = -num;
            }
            if (num == 0)
            {
                num = row._index - row2._index;
            }
            return num;
        }

        internal void UpdateFlags(SortFlags order)
        {
            this._ignoreCase = (order & SortFlags.IgnoreCase) != SortFlags.None;
            this._asDisplayed = (order & SortFlags.AsDisplayed) != SortFlags.None;
            this._reverse = (order & SortFlags.Descending) != SortFlags.None;
            this._noSort = (order & (SortFlags.Descending | SortFlags.Ascending)) == SortFlags.None;
        }
    }
}

